package com.sixbing.spider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixbing.spider.bean.CollegeBean;
import com.sixbing.spider.bean.User;
import com.sixbing.spider.service.ICollegeService;
import com.sixbing.spider.service.IUserService;
import com.sixbing.spider.utils.MyUtils;
import com.sixbing.spider.utils.ResponseUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/college")
public class CollegeController {
    private int max=100000;

    @Resource
    private ICollegeService iCollegeService;

    @RequestMapping("/getAllCollegeByProvinceId.do")
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int page = MyUtils.getIntOrDefaut(request, "page", 1);
        int pageSize = MyUtils.getIntOrDefaut(request, "pageSize", max);
        String strId = request.getParameter("provinceId");
        if (StringUtils.isEmpty(strId)) {
            ResponseUtils.ResponseError(response, 400, "id is null");
            return;
        } else {
            try {
                int provinceId = Integer.parseInt(strId);
                PageHelper.startPage(page, pageSize);
                List<CollegeBean> list = this.iCollegeService.getAllCollegeByProvinceId(provinceId);
                PageInfo<CollegeBean> pi = new PageInfo<>(list);
                ResponseUtils.Response(response, pi);
            } catch (Exception e) {
                ResponseUtils.ResponseError(response, 404, "服务器内部错误");
                e.printStackTrace();
            }
        }
    }


}
